#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <pthread.h>
int main(int argc,char* argv[])
{
	if(argc < 2)
	{
		printf("incorrect parametr\n");
		return 1;
	}
	/*
 	 * 1. 创建socket
 	 * SOCK_DGRAM : 使用UDP传输
 	 */
	int client_fd = socket(AF_INET,SOCK_DGRAM,0);
	

	/*
 	 * 3. 设置服务器的ip及端口信息
 	 */
	struct sockaddr_in server_attr;
	server_attr.sin_family = AF_INET;
	server_attr.sin_port = htons((short)atoi(argv[2]));
	inet_pton(AF_INET,argv[1],&server_attr.sin_addr.s_addr);
	char res[512] = {'\0'};
	while(1)
	{
		scanf("%s",res);
		if(sendto(client_fd,res,sizeof(res),0,(struct sockaddr*)&server_attr,sizeof(server_attr)) <= 0)
		{
			perror("sendto");
			break;
		}
		if(recv(client_fd,res,sizeof(res),0) <= 0)
		{
			perror("recv");
			break;
		}
		printf("%s\n",res);
	}
	return 0;
}
